package com.cskaoyan.controller.userController;


import com.cskaoyan.model.bo.PageBo;
import com.cskaoyan.model.vo.BaseRespVo;
import com.cskaoyan.model.vo.allusersvo.*;
import com.cskaoyan.service.allusers.AllUsersService;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin")
public class UserController {
    private ObjectMapper objectMapper = new ObjectMapper();

    {
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }

    @Autowired
    AllUsersService allUsersService;

    @RequiresPermissions("admin:user:list")
    @GetMapping("/user/list")
    public BaseRespVo getUserList(PageBo pageBo, String username, String mobile) {
        Integer phone = null;
        try {

            if (mobile != null) {
                phone = Integer.parseInt(mobile);
            }
        } catch (NumberFormatException e) {
            return BaseRespVo.error(402, "userId或mobile参数有问题");
        }

        UsersListVo usersListVo = allUsersService.getUserList(pageBo, username, mobile);
        return BaseRespVo.ok(usersListVo);
    }

    @RequiresPermissions("admin:address:list")
    @GetMapping("/address/list")
    public BaseRespVo getAddressList(PageBo pageBo, String name, String userId) {
        Integer id = null;
        if (userId != null && !"".equals(userId)) {
            try {
                id = Integer.parseInt(userId);
            } catch (NumberFormatException e) {
                return BaseRespVo.error(402, "userId参数有问题");
            }
        }
        AddressListVo usersListVo = allUsersService.getAddressList(pageBo, name, id);
        return BaseRespVo.ok(usersListVo);
    }


    @RequiresPermissions("admin:collect:list")
    @GetMapping("/collect/list")
    public BaseRespVo getCollectList(PageBo pageBo, String userId, String valueId) {

        Integer usid = null;
        Integer valId = null;
        try {

            if (userId != null && !"".equals(userId)) {
                usid = Integer.parseInt(userId);
            }
            if (valueId != null && !"".equals(valueId)) {
                valId = Integer.parseInt(valueId);
            }

        } catch (NumberFormatException e) {
            return BaseRespVo.error(402, "userId或valueId参数有问题");
        }

        CollectListVo usersListVo = allUsersService.getCollectList(pageBo, usid, valId);
        return BaseRespVo.ok(usersListVo);
    }


    @RequiresPermissions("admin:footprint:list")
    @GetMapping("/footprint/list")
    public BaseRespVo getFoorprintList(PageBo pageBo, String userId, String goodsId) {

        Integer usid = null;
        Integer goodId = null;
        if (userId != null && !"".equals(userId)) {
            try {
                usid = Integer.parseInt(userId);
                goodId = Integer.parseInt(goodsId);
            } catch (NumberFormatException e) {
                return BaseRespVo.error(402, "userId或goodsId参数有问题");
            }
        }

        FoorprintListVo usersListVo = allUsersService.getFoorprintList(pageBo, usid, goodId);
        return BaseRespVo.ok(usersListVo);
    }

    @RequiresPermissions("admin:history:list")
    @GetMapping("/history/list")
    public BaseRespVo getHistoryList(PageBo pageBo, String userId, String keyword) {

        Integer id = null;
        if (userId != null && !"".equals(userId)) {
            try {
                id = Integer.parseInt(userId);
            } catch (NumberFormatException e) {
                return BaseRespVo.error(402, "userId参数有问题");
            }
        }

        HistoryListVo usersListVo = allUsersService.getHistoryList(pageBo, id, keyword);
        return BaseRespVo.ok(usersListVo);
    }


    @RequiresPermissions("admin:feedback:list")
    @GetMapping("/feedback/list")
    public BaseRespVo getFeedbackList(PageBo pageBo, String id, String username) {

        Integer userId = null;
        if (id != null && !"".equals(id)) {
            try {
                userId = Integer.parseInt(id);
            } catch (NumberFormatException e) {
                return BaseRespVo.error(402, "userId或mobile（必须为11位）参数有问题");
            }
        }

        FeedbackLisVo usersListVo = allUsersService.getFeedbackList(pageBo, userId, username);
        System.out.println();
        return BaseRespVo.ok(usersListVo);
    }


}
